import {  syncRequest}from '@/utils/httpRequet'
import store from '@/store'
import Layout from '@/layout'
import { MessageBox, Message } from 'element-ui';
export function getComponent(url) {
  url=url.replace(/\.vue$/g,"");
  let start = url.split("/")[0];

  switch (start) {
  case 'system':
    return (resolve) => require(["@/system" + url.replace(start, "").replace(".vue","") + ".vue"], resolve);
  case 'mainLayout':
    return (resolve) => require(["@/mainLayout" + url.replace(start, "").replace(".vue","") + ".vue"], resolve);
  case 'views':
    return (resolve) => require(["@/views" + url.replace(start, "").replace(".vue","") + ".vue"], resolve);
  case 'kh':
    return (resolve) => require(["@/kh" + url.replace(start, "").replace(".vue","") + ".vue"], resolve);
  case 'fremwork':
    return (resolve) => require(["@/fremwork" + url.replace(start, "").replace(".vue","") + ".vue"], resolve);
  case 'appraise':
	return (resolve) => require(["@/appraise" + url.replace(start, "").replace(".vue","") + ".vue"], resolve);
  }
}
export function loadroot(defaultRoutes) {

  let searchs = window.location.href.split('?');
  if (searchs.length == 2) {
    let search = searchs[1].split("&");
    let root = '';
    search.map(item => {
      let info = item.split("=");
      if (info[0] == 'root') {
        root = info[1];
        return false;
      }
    })
    if (root != '') {
      let menusdata = syncRequest({
        url: 'loadMenu',
        data: {
          root: root
        }
      });
      if (menusdata.menu) {
        let rootmenu = menusdata.menu;
        let children = rootmenu.children;
        let isactive = false;
        function dealRoutes(menu) {
          if (menu.children) {
            if (menu.children.length == 0) {
              delete menu.children
            } else
              menu.children.map(item => {
                if (!isactive) {
                  item.meta = item.meta || {};
                  item.meta.activeMenu = true;
                  isactive = true;
                }
                dealRoutes(item);
              })

          }
          if(menu.pageConfig){
            try{
              let pageConfig=eval(`(${menu.pageConfig})`);
              menu.pageConfig=pageConfig;
            }catch(e){
              delete menu.pageConfig;
              Message({type:'error',message:`菜单【${menu.title}】页面配置参数错误`,duration:200})
            }
          }
          let url = menu.url;
          url = url.charAt(0) == '/' ? url.substr(1) : url;
          if (url == 'layout') {
            menu.component = Layout
              //   menu.redirect="/"+menu.path+"/"+menu.children[0].path;
          } else {

            menu.component = getComponent(url)
          }
          menu.meta = {
            title: menu.title,
            icon: menu.icon
          }
          menu.path = "/" + menu.path;
        }
        dealRoutes(rootmenu)
        children[0].meta = children[0].meta || {};
        children[0].meta.activeMenu = true;
        children[0].path = 'index'
        defaultRoutes[defaultRoutes.length - 1].children = children;
        children[0].open=true;
        // setTimeout(() => {
        //   store.dispatch("permission/setRouters",dealRoutes)
        // }, 10);

        // defaultRoutes[defaultRoutes.length-1].children.push(...children)
        children.map(item => {
          defaultRoutes.push(item)
        })
        // debugger
        // defaultRoutes[defaultRoutes.length-1].children.concat(...children)
      }

    }
  }
}
export function loadroot2(root) {

  if (root != '') {
    let menusdata = syncRequest({
      url: 'loadMenu',
      data: {
        root: root
      }
    });
    if (menusdata.menu) {
      let rootmenu = menusdata.menu;
      store.dispatch("business/setValues",{root:'rootmenu',values:rootmenu})
      let children = rootmenu.children;
      let isactive = false;
      let optoins=[]
      let menus={};
      function dealRoutes(menu,path_) {
        let url = menu.url;
        url = url.charAt(0) == '/' ? url.substr(1) : url;
        if (url == 'layout') {
          menu.component = Layout
            //   menu.redirect="/"+menu.path+"/"+menu.children[0].path;
        } else {

          menu.component = getComponent(url)
        }
        menu.meta = {
          title: menu.title,
          icon: menu.icon
        }
        if(menu.pageConfig){
          try{
            let pageConfig=eval(`(${menu.pageConfig})`);
            menu.pageConfig=pageConfig;
            if(pageConfig.defaultOpen===true){
                if(menu.parentmenu!=undefined){
                  menu.parentmenu.isOpenmenu=true;
                }else{
                  menu.isOpenmenu=true;
                }
            }
          }catch(e){
            delete menu.pageConfig;
            Message({type:'error',message:`菜单【${menu.title}】页面配置参数错误`,duration:200})
          }
         
        }
        menu.path = menu.path?( menu.path[0]=='/'?menu.path:("/" + menu.path)):"";
        optoins.push({
          path:path_+menu.path,
          name:menu.title,
          component:menu.component
        })
        if (menu.children) {
          if (menu.children.length == 0) {
            delete menu.children
          } else
            menu.children.map(item => {
              if (!isactive) {
                item.meta = item.meta || {};
                item.meta.activeMenu = true;
                isactive = true;
              }
              item.parentmenu=menu;
              dealRoutes(item,path_+menu.path);
            })

        }
        
      }
      dealRoutes(rootmenu,"")
      return {menu:rootmenu,optoins:optoins};
      // children[0].meta = children[0].meta || {};
      // children[0].meta.activeMenu = true;
      // children[0].path = 'index'
      //   defaultRoutes[defaultRoutes.length - 1].children = children;
      // // setTimeout(() => {
      // //   store.dispatch("permission/setRouters",dealRoutes)
      // // }, 10);

      // debugger
      // // defaultRoutes[defaultRoutes.length-1].children.push(...children)
      // children.map(item => {
      //   defaultRoutes.push(item)
      // })
      // debugger
      // defaultRoutes[defaultRoutes.length-1].children.concat(...children)
    }

  }
}
